<?php
/**
 * @package     gantry
 * @subpackage  features
 * @version		3.2.2 April 1, 2011
 * @author		RocketTheme http://www.rockettheme.com
 * @copyright 	Copyright (C) 2007 - 2011 RocketTheme, LLC
 * @license		http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
 *
 * Gantry uses the Joomla Framework (http://www.joomla.org), a GNU/GPLv2 content management system
 *
 */

defined('JPATH_BASE') or die();

gantry_import('core.gantryfeature');

/**
 * @package     gantry
 * @subpackage  features
 */
class GantryFeatureToTop extends GantryFeature {
    var $_feature_name = 'totop';
	
	function init() {
		global $gantry;
		
		$doc =& JFactory::getDocument();
		$getapps= & JFactory::getApplication();
		$bpath = JURI::base();
		$template = $getapps->getTemplate();

		if ($this->get('enabled')) {
			$doc->addScript('libraries/gantry/js/gantry-totop.js');
			$doc->addScript($bpath.'templates/'.$template.'/js/scrollspy.js');

			$script = "
			window.addEvent('domready',function() {
				/* smooth */
				new SmoothScroll({duration:500});
				
				/* link management */
				$$('#gantry-totop').set('opacity','0').setStyle('display','block');
				
				/* scrollspy instance */
				var ss = new ScrollSpy({
					min: 200,
					onEnter: function(position,state,enters) {
						$$('.gototop').fade('in');
					},
					onLeave: function(position,state,leaves) {
						$$('.gototop').fade('out');
					},
					container: window
				});
			});
			";
			$doc->addScriptDeclaration($script);
		}

	}
	
	function render($position="") {
	    ob_start();
	    ?>
		<div class="clear"></div>

		<a href="#" id="gantry-totop" class="gototop"><?php echo $this->get('text'); ?></a>

		<?php
	    return ob_get_clean();
	}
}